package mil.af.amc.adpetracker.web.actions.struts1.EquipmentItem.ajax;

import mil.af.amc.adpetracker.service.EquipmentItemService;
import mil.af.amc.adpetracker.service.ServiceException;
import mil.af.amc.adpetracker.web.actions.struts1.AbstractAction;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Simple AJAX action to send a User Status email.
 *
 * @author TSgt Jason Ferguson
 */

public class SendUserStatusEmail extends AbstractAction {

    private static final Log log = LogFactory.getLog(SendUserStatusEmail.class);

    private EquipmentItemService equipmentItemService;

    public void setEquipmentItemService(EquipmentItemService equipmentItemService) {
        this.equipmentItemService = equipmentItemService;
    }

    @Override
    public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {

        int userId = Integer.parseInt(httpServletRequest.getParameter("userId"));
        try {
            equipmentItemService.sendStatusEmail(userId);

            log.trace("Status message sent to userid " + userId);
            ActionMessage message = new ActionMessage("messages.mail.success");
            saveError(httpServletRequest, message);

        } catch (ServiceException e) {

            log.debug("Unable to send status email", e);
            ActionMessage message = new ActionMessage(e.getKey());
            saveError(httpServletRequest, message);
        }

        return actionMapping.getInputForward();

    }
}
